using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
namespace DAL
{
    public class tbDiaDiemDAL
    {
        private SqlConnection cn = null;
        public tbDiaDiemDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbDiaDiemDAL(tbDiaDiemDAL diadiem)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public void Insert(tbDiaDiem diadiem)
        {
            SqlCommand cm = new SqlCommand("sp_tbDiaDiem_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenDD", diadiem.TenDD);
            cm.Parameters.AddWithValue("@DiaChi", diadiem.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", diadiem.DienThoai);
            cm.Parameters.AddWithValue("@BanDo", diadiem.BanDo);
            cm.ExecuteNonQuery();
        }
        public void Update(tbDiaDiem diadiem)
        {
            SqlCommand cm = new SqlCommand("sp_tbDiaDiem_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDD", diadiem.MaDD);
            cm.Parameters.AddWithValue("@TenDD", diadiem.TenDD);
            cm.Parameters.AddWithValue("@DiaChi", diadiem.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", diadiem.DienThoai);
            cm.Parameters.AddWithValue("@BanDo", diadiem.BanDo);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tbDiaDiem_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDD", iD);
            cm.ExecuteNonQuery();
        }
        public tbDiaDiemCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbDiaDiem_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbDiaDiemCollection ddCollection = new tbDiaDiemCollection();

            while (dr.Read())
            {
                tbDiaDiem diadiem = new tbDiaDiem();
                diadiem.MaDD = int.Parse(dr.GetValue(0).ToString());
                diadiem.TenDD = dr.GetValue(1).ToString();
                diadiem.DiaChi = dr.GetValue(2).ToString();
                diadiem.DienThoai = dr.GetValue(3).ToString();
                diadiem.BanDo = dr.GetValue(4).ToString();
                ddCollection.Add(diadiem);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ddCollection;
        }
    }
}
